package com.hurl.waguji.biz.download;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCwZcfzbThsBO;
import com.hurl.waguji.persistence.JcsjCwZcfzbThsBOId;

class CwZcfzbThsProcessor extends BaseDownloadForUpdateProcessor {
	public CwZcfzbThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}
	private static final Logger log = Logger.getLogger(CwZcfzbThsProcessor.class);

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return ;
		}

		String gpdm = context.getEnvAsString("gpdm");
		
		String table = StringUtils.substringBetween(html, "<table","</table>");
		if(StringUtils.isBlank(table)){
			return ;
		}
		
		List<String> trlist = StringUtils.substringsBetween(table, "<tr", "</tr>");
		if(trlist==null || trlist.isEmpty()){
			return ;
		}
		//年度
		String tr = trlist.get(0);
		List<String> tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Date> list1 = new ArrayList<Date>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String nd = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list1.add(Formatter.toDate(nd));
		}
		//资产负债率
		tr = trlist.get(1);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list2 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list2.add(Formatter.toDouble(td));
		}
		//股东权益比例
		tr = trlist.get(2);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list3 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list3.add(Formatter.toDouble(td));
		}
		//资产总计(万元)
		tr = trlist.get(3);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list4 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list4.add(Formatter.toDouble(td));
		}
		//股东权益合计(万元)
		tr = trlist.get(4);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list5 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list5.add(Formatter.toDouble(td));
		}
		//负债合计(万元)
		tr = trlist.get(5);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list6 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list6.add(Formatter.toDouble(td));
		}
		//固定资产净额(万元)
		tr = trlist.get(6);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list7 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list7.add(Formatter.toDouble(td));
		}
		//无形资产(万元)
		tr = trlist.get(7);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list8 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list8.add(Formatter.toDouble(td));
		}
		//实收资本(万元)
		tr = trlist.get(8);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list9 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list9.add(Formatter.toDouble(td));
		}
		//资本公积金(万元)
		tr = trlist.get(9);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list10 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list10.add(Formatter.toDouble(td));
		}
		//盈余公积(万元)
		tr = trlist.get(10);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list11 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list11.add(Formatter.toDouble(td));
		}
		//未分配利润(万元)
		tr = trlist.get(11);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list12 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list12.add(Formatter.toDouble(td));
		}
		//货币资金(万元)
		tr = trlist.get(12);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list13 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list13.add(Formatter.toDouble(td));
		}
		//应收账款净额(万元)
		tr = trlist.get(13);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list14 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list14.add(Formatter.toDouble(td));
		}
		//存货（净额）(万元)
		tr = trlist.get(14);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list15 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list15.add(Formatter.toDouble(td));
		}
		//流动资产合计(万元)
		tr = trlist.get(15);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list16 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list16.add(Formatter.toDouble(td));
		}
		//流动比率
		tr = trlist.get(16);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list17 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list17.add(Formatter.toDouble(td));
		}
		//速动比率
		tr = trlist.get(17);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list18 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list18.add(Formatter.toDouble(td));
		}
		//其他应收款(万元)
		tr = trlist.get(18);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list19 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list19.add(Formatter.toDouble(td));
		}
		//流动负债合计(万元)
		tr = trlist.get(19);
		tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
		if(tdlist==null || tdlist.size()<=1){
			return ;
		}
		List<Double> list20 = new ArrayList<Double>(tdlist.size()-1);
		for(int i=1;i<tdlist.size();i++){
			String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
			list20.add(Formatter.toDouble(td));
		}
		int size = list1.size()+list2.size()+list3.size()+list4.size()+list5.size()
		+list6.size()+list7.size()+list8.size()+list9.size()+list10.size()
		+list11.size()+list12.size()+list13.size()+list14.size()+list15.size()
		+list16.size()+list17.size()+list18.size()+list19.size()+list20.size();
		if(size!=list1.size()*20 || size!=list2.size()*20 || size!=list3.size()*20 || size!=list4.size()*20 || size!=list5.size()*20
				 || size!=list6.size()*20 || size!=list7.size()*20 || size!=list8.size()*20 || size!=list9.size()*20 || size!=list10.size()*20
				 || size!=list11.size()*20 || size!=list12.size()*20 || size!=list13.size()*20 || size!=list14.size()*20 || size!=list15.size()*20
				 || size!=list16.size()*20 || size!=list17.size()*20 || size!=list18.size()*20 || size!=list19.size()*20 || size!=list20.size()*20){
			log.error("数据格式不对！");
			return;
		}
		JcsjCwZcfzbThsBO bo = null;
		for(int i=0;i<list1.size();i++){
			bo = new JcsjCwZcfzbThsBO();
			bo.setId(new JcsjCwZcfzbThsBOId(gpdm,list1.get(i)));
			bo.setZcfzl(list2.get(i)/100);
			bo.setGdqybl(list3.get(i)/100);
			bo.setZcze(list4.get(i)*10000);
			bo.setGdqyhj(list5.get(i)*10000);
			bo.setFzhj(list6.get(i)*10000);
			bo.setGdzcje(list7.get(i)*10000);
			bo.setWxzc(list8.get(i)*10000);
			bo.setSszb(list9.get(i)*10000);
			bo.setZbgjj(list10.get(i)*10000);
			bo.setYygj(list11.get(i)*10000);
			bo.setWfplr(list12.get(i)*10000);
			bo.setHbzj(list13.get(i)*10000);
			bo.setYszkje(list14.get(i)*10000);
			bo.setChje(list15.get(i)*10000);
			bo.setLdzchj(list16.get(i)*10000);
			bo.setLdbl(list17.get(i));
			bo.setSdbl(list18.get(i));
			bo.setQtysk(list19.get(i)*10000);
			bo.setLdfzhj(list20.get(i)*10000);
			bo.setLastupdate(currentTime);
			
			collector.addBO(bo);
		}
	
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/cwtable/Balance_declaredate.html";
			}
		};
	}

}
